//
//  RegisterViewController.m
//  E_ChatRoom
//
//  Created by rimi1 on 15/6/27.
//  Copyright (c) 2015年 TJ. All rights reserved.
//

#import "RegisterViewController.h"

@interface RegisterViewController ()<UITextFieldDelegate>

/*手机号输入框*/
@property (nonatomic, strong)UITextField *phoneNumFiled;
/*密码输入框*/
@property (nonatomic, strong)UITextField *pswFiled;
/*注册*/
@property (nonatomic, strong)UIButton *registBtn;
@end


@implementation RegisterViewController

/**
 *  重写init方法
 */
- (instancetype)init
{
    self = [super init];
    if (self) {
        self.view.backgroundColor = [UIColor lightGrayColor];
        self.title = @"注册";
        self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(dismissSelf)];
        self.navigationItem.leftBarButtonItem.tintColor = [UIColor whiteColor];
    }
    return self;
}

#pragma mark - life circle methods

- (void)viewDidLoad {
    
    [super viewDidLoad];
   
    [self initializeUserInterface];
}

#pragma mark - textFiled Delegate method (textFiled代理方法)

/**
 *  退出键盘
 */
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    
    [textField resignFirstResponder];
    return YES;
}


#pragma mark - private methods（私有方法）

/**
 *  初始化用户界面
 */
- (void)initializeUserInterface{
    
    //0.手机号码输入框
    CGFloat phoneNumFiledX = 40*Ratio;
    CGFloat phoneNumFiledY = 70*Ratio;
    CGFloat phoneNumFiledW = (ScreenWidth - phoneNumFiledX*2);
    CGFloat phoneNumFiledH = 28*Ratio;
    _phoneNumFiled = [[UITextField alloc] initWithFrame:CGRectMake(phoneNumFiledX, phoneNumFiledY, phoneNumFiledW, phoneNumFiledH)];
    _phoneNumFiled.delegate = self;
    _phoneNumFiled.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_phoneNumFiled];
    
    //1.密码输入框
    CGFloat pswFiledX = phoneNumFiledX;
    CGFloat pswFiledY = CGRectGetMaxY(_phoneNumFiled.frame) + 20*Ratio;
    CGFloat pswFiledW = (ScreenWidth - pswFiledX*2);
    CGFloat pswFiledH = 28*Ratio;
    _pswFiled = [[UITextField alloc] initWithFrame:CGRectMake(pswFiledX, pswFiledY, pswFiledW, pswFiledH)];
    _pswFiled.delegate = self;
    _pswFiled.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_pswFiled];
    
    //2.注册
    CGFloat registBtnW = pswFiledW;
    CGFloat registBtnH = 30*Ratio;
    CGFloat registBtnX = pswFiledX;
    CGFloat registBtnY = CGRectGetMaxY(_pswFiled.frame) + 30*Ratio;
    _registBtn = [[UIButton alloc] initWithFrame:CGRectMake(registBtnX, registBtnY, registBtnW, registBtnH)];
    [_registBtn setTitle:@"注册" forState:UIControlStateNormal];
    [_registBtn setResizeN_BG:@"fts_green_btn" H_BG:@"fts_green_btn_HL"];
    _registBtn.titleLabel.font = [UIFont systemFontOfSize:14];
    _registBtn.titleLabel.textAlignment = NSTextAlignmentCenter;
    [_registBtn addTarget:self action:@selector(regist) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_registBtn];
}

/**
 *  注册
 */
- (void)regist{
    
    [self.view endEditing:YES];
    
    // 判断用户输入的是否为手机号码
    if (![self.phoneNumFiled isTelphoneNum]) {
        [MBProgressHUD showError:@"请输入正确的手机号码" toView:self.view];
        return;
    }
    
    // 1.把用户注册的数据保存单例
    UserInfo *userInfo = [UserInfo sharedUserInfo];
    userInfo.registerUser = self.phoneNumFiled.text;
    userInfo.registerPwd = self.pswFiled.text;
    
    
    
    // 2.调用WCXMPPTool的xmppUserRegister
    
    [XMPPTool sharedXMPPTool].registerOperation = YES;
    
    // 提示
    
    [MBProgressHUD showMessage:@"正在注册中....." toView:self.view];
    
    __weak typeof(self) selfVc = self;
    [[XMPPTool sharedXMPPTool] xmppUserRegister:^(XMPPResultType type) {
        
        [selfVc handleResultType:type];
    }];
}

/**
 *  处理注册的结果
 */
-(void)handleResultType:(XMPPResultType)type{
    
    dispatch_async(dispatch_get_main_queue(), ^{
        [MBProgressHUD hideHUDForView:self.view];
        switch (type) {
            case XMPPResultTypeNetErr:
                [MBProgressHUD showError:@"网络不稳定" toView:self.view];
                break;
            case XMPPResultTypeRegisterSuccess:
                [MBProgressHUD showError:@"注册成功" toView:self.view];
                // 回到上个控制器
                [self dismissSelf];
                
                if ([self.delegate respondsToSelector:@selector(regisgerViewControllerDidFinishRegister)]) {
                    [self.delegate regisgerViewControllerDidFinishRegister];
                }
                break;
                
            case XMPPResultTypeRegisterFailure:
                [MBProgressHUD showError:@"注册失败,用户名重复" toView:self.view];
                break;
            default:
                break;
        }
    });
    
    
}

/**
 *  点击空白处，退出键盘
 */
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    
    [self.view endEditing:YES];
}

/**
 *  返回上一级
 */
- (void)dismissSelf{
    
    [self.presentingViewController dismissViewControllerAnimated:YES completion:nil];
}


@end
